<template>
	<scroll-view scroll-y="true" class="scroll-Y">
		<fanhui :title='playlist.name'></fanhui>
		<view class="list-head" :style="{'background-image':'url('+ playlist.coverImgUrl +')'}">
			<view class="list-head-img">
				<image :src="playlist.coverImgUrl" mode=""></image>
				<text class="iconfont iconyousanjiao">{{ playlist.playCount | formatPlayCount }}</text>
			</view>
			<view class="list-head-text">
				<view>
					{{playlist.name}}
				</view>
				<view>
					<image v-if="playlist.creator" :src="playlist.creator.avatarUrl" mode=""></image>
					<text v-if="playlist.creator">{{ playlist.creator.nickname }}</text>
				</view>

				<view>
					{{ playlist.description }}
				</view>
			</view>
		</view>
		<view v-show="showPlaylist" class="playlist-title">
			<text class="iconfont icon-bofangxian"></text>
			<text>播放全部</text>
			<text>(共{{ playlist.trackCount }}首)</text>
		</view>
		<view class="playlist">
			<view class="playlist-item" v-for="(item, index) in playlist.tracks" :key="item.id"
				@click="navPlayer(item.id)">
				<view class="playlist-top">{{ index + 1 }}</view>
				<view class="playlist-song">
					<view>{{ item.name }}</view>
					<view>
						<image v-if="privileges[index].flag > 60 && privileges[index].flag < 70"
							src="../../static/dujia.png"></image>
						<image v-if="privileges[index].maxbr == 999000" src="../../static/sq.png"></image>
						{{ item.ar[0].name }} - {{ item.name }}
					</view>

				</view>
				<view class="bofang">
					<text class="iconfont icon-bofangxian"></text>
				</view>
			</view>
		</view>
	</scroll-view>
</template>

<script>
	import fanhui from "../../components/fanhui.vue"
	import {
		list,
		songDetail,
		songUrl
	} from '../../common/api.js'
	export default {
		data() {
			return {
				playlist: {
					trackCount: '',
					title: '',
				},
				privileges: [],
				showPlaylist: false,

			}
		},
		onLoad(options) {
			// 从传入的参数中获取列表ID
			let listid = options.listid;
			// 调用list函数并处理返回的数据
			list(listid).then((res) => {
				// 检查返回数据的状态码是否为'200'
				if (res.data.code == '200') {
					// 将返回的歌单数据和权限数据赋给对应的变量
					this.playlist = res.data.playlist;
					this.privileges = res.data.privileges;
					// 设置标记，显示歌单信息
					this.showPlaylist = true;
				}
			});
		},
		filters: {
			formatPlayCount(playCount) {
				if (playCount >= 10000 && playCount < 100000000) {
					playCount /= 10000;
					return playCount.toFixed(1) + '万';
				} else if (playCount >= 100000000) {
					playCount /= 100000000;
					return playCount.toFixed(1) + '亿';
				} else {
					return playCount;
				}
			}
		},
		components: {
			fanhui
		},
		methods: {
			navPlayer(id) {
				// 在导航到播放器页面前获取歌曲链接并传递给播放器页面
				songUrl(id).then((res) => {
					const url = res.data.data[0].url;
					uni.navigateTo({
						url: '/pages/player/player?songId=' + id
					});
				}).catch((error) => {
					console.error('获取歌曲链接失败', error);
				});
			}
		}
	}
</script>

<style scoped>
	.list-head {
		display: flex;
		padding: 100rpx 0;
	}

	.list-head-img {
		width: 264rpx;
		height: 264rpx;
		border-radius: 20rpx;
		overflow: hidden;
		position: relative;
		margin-left: 26rpx;
		margin-right: 42rpx;
	}

	.list-head-img image {
		width: 100%;
		height: 100%;
	}

	.list-head-img text {
		position: absolute;
		right: 8rpx;
		top: 8rpx;
		color: white;
		font-size: 26rpx;
	}

	.list-head-text {
		flex: 1;
		font-size: 24rpx;
		color: #e1e2e3;
	}

	.list-head-text view:nth-child(1) {
		font-size: 34rpx;
		color: #ffffff;
	}

	.list-head-text view:nth-child(2) {
		margin-top: 30rpx;
		display: flex;
		align-items: center;
		color: #ffffff;
	}

	.list-head-text view:nth-child(2) image {
		margin-top: 3rpx;
		margin-right: 8rpx;
		width: 50rpx;
		height: 50rpx;
		border-radius: 50%;
		font-size: 28rpx
	}

	.list-head-text view:nth-child(3) {
		display: flex;
		margin: 20rpx 0;
		color: #ffffff;
	}

	.list-head-text view:nth-child(4) {
		line-height: 34rpx;
		font-size: 22rpx;
	}

	.playlist-title {
		display: flex;
		align-items: center;
	}

	.playlist-title text:nth-child(1) {
		margin: 30rpx 0rpx 30rpx 30rpx;
		font-size: 46rpx
	}

	.playlist-title text:nth-child(2) {
		margin: 30rpx 10rpx 30rpx 10rpx;
	}

	.playlist-title text:nth-child(3) {
		font-size: 28rpx;
		color: #8f8f8f;
	}

	.playlist-item {
		display: flex;
		margin: 20rpx 30rpx;
		align-items: center;
	}

	.playlist-top {
		width: 56rpx;
		font-size: 28rpx;
		color: #979797;
	}

	.playlist-song {
		flex: 1;
		line-height: 40rpx;
	}

	.playlist-song view:nth-child(1) {
		font-size: 30rpx;
		width: 70vw;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.playlist-song view:nth-child(2) {
		font-size: 22rpx;
		color: #a2a2a2;
		width: 70vw;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.playlist-song image {
		width: 34rpx;
		height: 22rpx;
		margin-right: 10rpx;
	}

	.bofang {
		color: #a2a2a2;
	}
</style>